package org.example.l0;

import org.junit.jupiter.api.Test;

public class myPow {
    @Test
    void test() {
        double v = myPow(4, -2);
        System.out.println(v);
    }

    public double myPow(double x,
                        int n) {
        return n >= 0 ? myPow2(x, (long) n) : 1 / myPow2(x, -(long) n);
    }

    public double myPow2(double x,
                         long n) {
        if (n == 0) {
            return 1;
        }

        double v = myPow2(x, n >> 1);
        return n % 2 == 0 ? v * v : v * v * x;
    }

}
